class Solution {
public:
    vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
        vector<vector<int>> edges(numCourses);
        vector<int> in(numCourses);

        for(auto& v:prerequisites)
        {
            int a=v[0],b=v[1];
            edges[b].push_back(a);
            in[a]++;
        }

        queue<int> q;
        vector<int> ret;
        for(int i=0;i<numCourses;i++)
        {
            if(in[i]==0)q.push(i);
        }
        while(q.size())
        {
            int t=q.front();q.pop();
            ret.push_back(t);
            for(int a:edges[t])
            {
                in[a]--;
                if(in[a]==0)q.push(a);
            }
        }

        if(ret.size()==numCourses)return ret;
        else return {};
    }
};
